<?php
/*
 * $Id: ValidatorTest.class.php 157 2009-12-16 15:59:18Z namba0219 $
 * =============================================================================
 * Barge - web application framework on PHP5
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * =============================================================================
 * For more information, see http://barge.googlecode.com/
 */
namespace barge\web;

use \UnitTestCase;

use barge\System;
use barge\utils\ArrayList;
use barge\utils\HashMap;
use barge\utils\IndexOutOfBoundsException;
use barge\utils\IllegalAccessException;
use barge\utils\IllegalArgumentException;
use barge\web\Validator;

/**
 * @see UnitTestCase
 */
class ValidatorTest extends UnitTestCase
{
	/**
	 * @test
	 */
	public function testIsAlphaNumerics()
	{
		$this->assertIdential(Validator::isAlphaNumerics(null), false);
		$this->assertIdential(Validator::isAlphaNumerics(''), false);
		$this->assertIdential(Validator::isAlphaNumerics('1234567890abcdefghijklmnopqrstuvwsyzABCDEFGHIJKLMNOPQRSTUVWSYZ'), true);
		$this->assertIdential(Validator::isAlphaNumerics('!'), false);
		$this->assertIdential(Validator::isAlphaNumerics('"'), false);
		$this->assertIdential(Validator::isAlphaNumerics(' '), false);
		$this->assertIdential(Validator::isAlphaNumerics('#'), false);
		$this->assertIdential(Validator::isAlphaNumerics('$'), false);
		$this->assertIdential(Validator::isAlphaNumerics('%'), false);
		$this->assertIdential(Validator::isAlphaNumerics('&'), false);
		$this->assertIdential(Validator::isAlphaNumerics('\''), false);
		$this->assertIdential(Validator::isAlphaNumerics('('), false);
		$this->assertIdential(Validator::isAlphaNumerics(')'), false);
		$this->assertIdential(Validator::isAlphaNumerics('='), false);
		$this->assertIdential(Validator::isAlphaNumerics('-'), false);
		$this->assertIdential(Validator::isAlphaNumerics('^'), false);
		$this->assertIdential(Validator::isAlphaNumerics('@'), false);
		$this->assertIdential(Validator::isAlphaNumerics('{'), false);
		$this->assertIdential(Validator::isAlphaNumerics('}'), false);
		$this->assertIdential(Validator::isAlphaNumerics('*'), false);
		$this->assertIdential(Validator::isAlphaNumerics('+'), false);
		$this->assertIdential(Validator::isAlphaNumerics('<'), false);
		$this->assertIdential(Validator::isAlphaNumerics('>'), false);
		$this->assertIdential(Validator::isAlphaNumerics('?'), false);
		$this->assertIdential(Validator::isAlphaNumerics('`'), false);
		$this->assertIdential(Validator::isAlphaNumerics('\\'), false);
		$this->assertIdential(Validator::isAlphaNumerics(':'), false);
		$this->assertIdential(Validator::isAlphaNumerics(';'), false);
		$this->assertIdential(Validator::isAlphaNumerics('ｱｲｳｴｵ'), false);
		$this->assertIdential(Validator::isAlphaNumerics('あいうえお'), false);
		$this->assertIdential(Validator::isAlphaNumerics(null), false);
	}

	/**
	 * @test
	 */
	public function testIsAlphas()
	{
		$this->assertIdential(Validator::isAlphas(null), false);
		$this->assertIdential(Validator::isAlphas(''), false);
		$this->assertIdential(Validator::isAlphas('abcdefghijklmnopqrstuvwsyzABCDEFGHIJKLMNOPQRSTUVWSYZ'), true);
		$this->assertIdential(Validator::isAlphas('1'), false);
		$this->assertIdential(Validator::isAlphas('2'), false);
		$this->assertIdential(Validator::isAlphas('3'), false);
		$this->assertIdential(Validator::isAlphas('4'), false);
		$this->assertIdential(Validator::isAlphas('5'), false);
		$this->assertIdential(Validator::isAlphas('6'), false);
		$this->assertIdential(Validator::isAlphas('7'), false);
		$this->assertIdential(Validator::isAlphas('8'), false);
		$this->assertIdential(Validator::isAlphas('9'), false);
		$this->assertIdential(Validator::isAlphas('0'), false);
		$this->assertIdential(Validator::isAlphas('!'), false);
		$this->assertIdential(Validator::isAlphas('"'), false);
		$this->assertIdential(Validator::isAlphas(' '), false);
		$this->assertIdential(Validator::isAlphas('#'), false);
		$this->assertIdential(Validator::isAlphas('$'), false);
		$this->assertIdential(Validator::isAlphas('%'), false);
		$this->assertIdential(Validator::isAlphas('&'), false);
		$this->assertIdential(Validator::isAlphas('\''), false);
		$this->assertIdential(Validator::isAlphas('('), false);
		$this->assertIdential(Validator::isAlphas(')'), false);
		$this->assertIdential(Validator::isAlphas('='), false);
		$this->assertIdential(Validator::isAlphas('-'), false);
		$this->assertIdential(Validator::isAlphas('^'), false);
		$this->assertIdential(Validator::isAlphas('@'), false);
		$this->assertIdential(Validator::isAlphas('{'), false);
		$this->assertIdential(Validator::isAlphas('}'), false);
		$this->assertIdential(Validator::isAlphas('*'), false);
		$this->assertIdential(Validator::isAlphas('+'), false);
		$this->assertIdential(Validator::isAlphas('<'), false);
		$this->assertIdential(Validator::isAlphas('>'), false);
		$this->assertIdential(Validator::isAlphas('?'), false);
		$this->assertIdential(Validator::isAlphas('`'), false);
		$this->assertIdential(Validator::isAlphas('\\'), false);
		$this->assertIdential(Validator::isAlphas(':'), false);
		$this->assertIdential(Validator::isAlphas(';'), false);
		$this->assertIdential(Validator::isAlphas('ｱｲｳｴｵ'), false);
		$this->assertIdential(Validator::isAlphas('あいうえお'), false);
		$this->assertIdential(Validator::isAlphas(null), false);
	}

//		/**
//	 * @test
//	 */
//	public function testIsDate()
//	{
//		System::setProperty('Validator', 'Formats', 'date', '|^(\d{4])/(\d{2])/(\d{2})$|');
//		$this->assertTrue(Validator::isDate(null) === false);
//		$this->assertTrue(Validator::isDate('abcdefg') === false);
//		$this->assertTrue(Validator::isDate('20081011') === false);
//		$this->assertTrue(Validator::isDate('2008-10-11') === false);
//		$this->assertTrue(Validator::isDate('2008/10/11') === true);
//		$this->assertTrue(Validator::isDate('2008/03/32') === false);
//		$this->assertTrue(Validator::isDate('2008/02/30') === false);
//	}

	/**
	 * @test
	 */
	public function testIsMail()
	{
//		System::setProperty('Validator', 'Formats', 'mail', '|^\w+@\w+.\w+$|');
//		$this->assertTrue(Validator::isMail(null) === false);
//		$this->assertTrue(Validator::isMail('sample@sample.co.jp') === false);
		// TODO :
	}

	/**
	 * @test
	 */
	public function testIsNumerics()
	{
		$this->assertTrue(Validator::isNumerics(null) === false);
		$this->assertTrue(Validator::isNumerics('1') === true);
		$this->assertTrue(Validator::isNumerics('12') === true);
		$this->assertTrue(Validator::isNumerics('123') === true);
		$this->assertTrue(Validator::isNumerics('AAA') === false);
		$this->assertTrue(Validator::isNumerics('AA1') === false);
		$this->assertTrue(Validator::isNumerics('!') === false);
		$this->assertTrue(Validator::isNumerics('"') === false);
		$this->assertTrue(Validator::isNumerics(' ') === false);
		$this->assertTrue(Validator::isNumerics('#') === false);
		$this->assertTrue(Validator::isNumerics('$') === false);
		$this->assertTrue(Validator::isNumerics('%') === false);
		$this->assertTrue(Validator::isNumerics('&') === false);
		$this->assertTrue(Validator::isNumerics('\'') === false);
		$this->assertTrue(Validator::isNumerics('(') === false);
		$this->assertTrue(Validator::isNumerics(')') === false);
		$this->assertTrue(Validator::isNumerics('=') === false);
		$this->assertTrue(Validator::isNumerics('-') === false);
		$this->assertTrue(Validator::isNumerics('^') === false);
		$this->assertTrue(Validator::isNumerics('@') === false);
		$this->assertTrue(Validator::isNumerics('{') === false);
		$this->assertTrue(Validator::isNumerics('}') === false);
		$this->assertTrue(Validator::isNumerics('*') === false);
		$this->assertTrue(Validator::isNumerics('+') === false);
		$this->assertTrue(Validator::isNumerics('<') === false);
		$this->assertTrue(Validator::isNumerics('>') === false);
		$this->assertTrue(Validator::isNumerics('?') === false);
		$this->assertTrue(Validator::isNumerics('`') === false);
		$this->assertTrue(Validator::isNumerics('\\') === false);
		$this->assertTrue(Validator::isNumerics(':') === false);
		$this->assertFalse(Validator::isNumerics(';'));
		$this->assertFalse(Validator::isNumerics('ｱｲｳｴｵ'));
		$this->assertFalse(Validator::isNumerics('あいうえお'));
		$this->assertFalse(Validator::isNumerics(null));
	}

	/**
	 * @test
	 */
	public function testIsPresent()
	{
		$this->assertFalse(Validator::isPresent(null));
		$this->assertFalse(Validator::isPresent(''));
		$this->assertFalse(Validator::isPresent(' '));
		$this->assertFalse(Validator::isPresent('  '));
		$this->assertTrue(Validator::isPresent('　'));
	}

	/**
	 * @test
	 */
	public function testIsTelno()
	{
		// TODO :
	}

	/**
	 * @test
	 */
	public function testIsTimestamp()
	{
		// TODO :
	}

	/**
	 * @test
	 */
	public function testIsValidFormat()
	{
		// TODO :
	}

	/**
	 * @test
	 */
	public function testIsValidLength()
	{
		$this->assertTrue(Validator::isValidLength(null, 0, 3) === true);
		$this->assertTrue(Validator::isValidLength('', 0, 3) === true);
		$this->assertTrue(Validator::isValidLength('a', 0, 3) === true);
		$this->assertTrue(Validator::isValidLength('a', 1, 3) === true);
		$this->assertTrue(Validator::isValidLength('a', 2, 3) === false);
		$this->assertTrue(Validator::isValidLength('ab', 0, 3) === true);
		$this->assertTrue(Validator::isValidLength('abc', 0, 3) === true);
		$this->assertTrue(Validator::isValidLength('abcd', 0, 3) === false);
		$this->assertTrue(Validator::isValidLength('あ', 0, 3) === true);
		$this->assertTrue(Validator::isValidLength('あい', 0, 3) === true);
		$this->assertTrue(Validator::isValidLength('あいう', 0, 3) === true);
		$this->assertTrue(Validator::isValidLength('あいう', 3, 3) === true);
		$this->assertTrue(Validator::isValidLength('あいう', 4, 3) === false);
		$this->assertTrue(Validator::isValidLength('あいうえ', 0, 3) === false);
//		$this->assertTrue(Validator::isValidLength('あ', 3) === true);
//		$this->assertTrue(Validator::isValidLength('あい', 3) === true);
//		$this->assertTrue(Validator::isValidLength('あいう', 3) === true);
//		$this->assertTrue(Validator::isValidLength('abcd', 3) === false);
	}


	/**
	 * @test
	 */
	public function testIsWithin()
	{
		$this->assertFalse(Validator::isWithin('2', new ArrayList('0', '1')));
		$this->assertTrue(Validator::isWithin('0', new ArrayList('0', '1')));
		$this->assertTrue(Validator::isWithin('1', new ArrayList('0', '1')));
		$this->assertFalse(Validator::isWithin('1', new ArrayList(0, 1)));
		$this->assertTrue(Validator::isWithin(1, new ArrayList(0, 1)));
		$this->assertTrue(Validator::isWithin('abc', new ArrayList('abc', 'def')));
	}
}

?>